﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emptiness.Screens;
using FlatRedBall.Input;
using Microsoft.Xna.Framework.Input;
using Emptiness;
using Emptiness.Utils;
using FlatRedBall;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math;
using FlatRedBall.Graphics;
using Microsoft.Xna.Framework.Audio;
using System.Threading;

namespace Emptiness.Screens
{
    public delegate void CreateTowerDelegate(List<Object> objectToLoad);

    public class MyCreateTower
    {
        public void CreateTower(List<Object> objectToLoad)
        {
            //while (executeThread)
            //{
            if (!GameScreen.stopAll)
            {
                EmptinessGame.BooScriptManager.ExecuteScript("BooBehaviour", "CreateTower", objectToLoad, false);
                //    Thread.Sleep(3000);
                //}
            }
                
        }
    }
    

    public class GameScreen : Screen
    {
        private Map map;
        private static List<Object> objectToLoad;
        const float MAX = 10;
        const float PACE = 1;
        bool reload = false;
        public static Space ball = null;
        private MyCreateTower myCreateTower;
        private CreateTowerDelegate createTower;
        private IAsyncResult result;
        private bool sending = false;
        public static bool areYouRelaxedScreenAnswer = false;
        public static bool stopAll = false;
        public static bool areYouRelaxedScreenAnswered = false;


        public override void Destroy()
        {
            ball.Destroy();
            map.RemoveTowers();
            base.Destroy();
        }

        public GameScreen()
            : base("", "GameScreen")
        {
            map = new Map();
            ball = new Space("Ball");
            objectToLoad = new List<object> { map, MathUtils.GetMathUtils(), ball};
            LoadMap();
            myCreateTower = new MyCreateTower();
            createTower = new CreateTowerDelegate(myCreateTower.CreateTower);
        }

        private void LoadMap()
        {
            EmptinessGame.BooScriptManager.ExecuteScript("BooBehaviour", "LoadMap", objectToLoad, true);
        }

        private void ChangeBG()
        {
            EmptinessGame.BooScriptManager.ExecuteScript("BooBehaviour", "ChangeBg", objectToLoad, reload);
            reload = false;
        }

        private void CreateTower()
        {
            //while (executeThread)
            //{
            EmptinessGame.BooScriptManager.ExecuteScript("BooBehaviour", "CreateTower", objectToLoad, false);
            //    Thread.Sleep(3000);
            //}

        }

       
        public override void Activity(bool firstTimeCalled)
        {
            base.Activity(firstTimeCalled);

            if (areYouRelaxedScreenAnswer)
            {
                //IsActivityFinished = true;
                Destroy();
                FlatRedBallServices.Game.Exit();
            }

            if (stopAll && areYouRelaxedScreenAnswered)
            {
                stopAll = false;
                areYouRelaxedScreenAnswered = false;
                SpriteManager.Camera.X = ball.X;
                SpriteManager.Camera.Y = ball.Y; 
            }


            if (!stopAll)
            {
                MoveBall();

                if (InputManager.Keyboard.KeyReleased(Keys.Enter))
                {
                    ChangeBG();
                }

                if (InputManager.Keyboard.KeyReleased(Keys.X))
                {
                    ball.XVelocity = 0;
                    ball.YVelocity = 0;
                    SpriteManager.Camera.XVelocity = 0;
                    SpriteManager.Camera.YVelocity = 0;
                    SpriteManager.Camera.X = 0;
                    SpriteManager.Camera.Y = 0;

                    bool loadNewLayer = true; // or true if you want it on a new layer
                    LoadPopup("Emptiness.Screens.AreYouRelaxedScreen", loadNewLayer);
                    stopAll = true;
                    if (areYouRelaxedScreenAnswer)
                    {
                        //IsActivityFinished = true;
                        Destroy();
                        FlatRedBallServices.Game.Exit();
                    }
                }

                if (InputManager.Keyboard.KeyReleased(Keys.Z))
                {
                    reload = true;
                    ChangeBG();
                }

                //ball.Activity();
                for (int i = 0; i < map.TowerList.Count; i++)
                {
                    var tower = map.TowerList[i];
                    if (ball.Collision.CollideAgainstMove(tower.Collision, ball.Mass, tower.Mass))
                    {
                        SpriteManager.Camera.X = ball.X;
                        SpriteManager.Camera.Y = ball.Y; 
                        map.PlaySound(i);
                        // map.RemoveTowerFromManagers(i);
                        // i--;
                        //TODO: Phrase
                    }

                }



                if (result == null && !sending && MathUtils.GetMathUtils().GetRandomNumberMaxNumber(100) < 75)
                {
                    result = createTower.BeginInvoke(objectToLoad, null, null);
                    sending = true;
                }
                else
                {
                    result = null;
                    sending = false;
                }


                map.VerifyTowerTimeToLive();
            }
            else 
            {
                ball.XVelocity = 0;
                ball.YVelocity = 0;
                SpriteManager.Camera.XVelocity = 0;
                SpriteManager.Camera.YVelocity = 0;
            }
           
        }

        private void MoveBall()
        {
            //if (InputManager.Keyboard.KeyReleased(Keys.W))
            //{
            //    if (ball.ZVelocity < MAX)
            //    {
            //        ball.ZVelocity += PACE;
            //        SpriteManager.Camera.ZVelocity += PACE;
            //    }
            //}
            //else if (InputManager.Keyboard.KeyReleased(Keys.S))
            //{
            //    if (ball.ZVelocity > -MAX)
            //    {
            //        ball.ZVelocity += -PACE;
            //        SpriteManager.Camera.ZVelocity += -PACE;
            //    }
                
            //}
            if (InputManager.Keyboard.KeyPushed(Keys.Right))
            {
                if (ball.XVelocity < MAX)
                {
                    ball.XVelocity += PACE;
                    SpriteManager.Camera.XVelocity += PACE;
                }
                    
            }
            else if (InputManager.Keyboard.KeyPushed(Keys.Left))
            {
                if (ball.XVelocity > -MAX)
                {
                    ball.XVelocity += -PACE;
                    SpriteManager.Camera.XVelocity += -PACE;
                }
                    
            }
            else if (InputManager.Keyboard.KeyPushed(Keys.Up))
            {
                if (ball.YVelocity < MAX)
                {
                    ball.YVelocity += PACE;
                    SpriteManager.Camera.YVelocity += PACE;
                }
                    
            }
            else if (InputManager.Keyboard.KeyPushed(Keys.Down))
            {
                if (ball.YVelocity > -MAX)
                {
                    ball.YVelocity += -PACE;
                    SpriteManager.Camera.YVelocity += -PACE;
                }
            }
            
        }
    }
}
